package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.service.ValidateCodeService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 代码使我快乐 Administrator on 2019/12/10.
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;

    @RequestMapping("/login" )
    public Result login(HttpServletResponse response, @RequestBody Map<String, String> map){

        try {
            String telephone = map.get("telephone");
            Result result = memberService.login(map.get("telephone"),map.get("validateCode"));
            if (result.isFlag()){
                Cookie cookie = new Cookie("login_member_telephone", telephone);
                cookie.setPath("/"); // 设置路径
                cookie.setMaxAge(60*60*24*7); //设置存活时间
                response.addCookie(cookie);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(false, MessageConstant.LOGIN_FALL);
    }
}
